package net.cctms.zhishu;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.alibaba.android.arouter.launcher.ARouter;

import net.cctms.zhishu.webAppFramework.contact.UrlName;
import net.cctms.zhishu.webAppFramework.model.PicEntity;
import net.cctms.zhishu.webAppFramework.ui.GestureImageActivity;
import net.cctms.zhishu.webAppFramework.ui.video.MyVideoActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import wendu.dsbridge.CompletionHandler;

/**
 * Created by huangdiudiu on 2018/5/7.
 */

public class JsApi {
    Context context;
    public JsApi(Context context) {
       this.context=context;
    }
    @JavascriptInterface
    public String testSyn(Object msg) {
        Log.e("backinfo", "testSyn");
       /* ARouter.getInstance().build("/zhishu/ArouterActivity").navigation();*/
        String url="http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4";
        ARouter.getInstance().
                build("/zhishu/MyVideoActivity").
                withString(UrlName.ARTICLE_ID,"111111").withString(UrlName.CONTENT_URL,url).withString(UrlName.ORIGINAL_URL,url).
                withString(UrlName.VIDEO_TITLE,"标题").withString(UrlName.VIDEO_URL,url).withString(UrlName.VIDEO_SUMMARY,"视频描述").withString(UrlName.PAGE_FROM,"视频来源").
                navigation();
        return msg + "［syn call］";

    }

    @JavascriptInterface
    public String image(Object msg) {
        ArrayList<PicEntity> picEntities = new ArrayList<>();
        PicEntity picEntity = new PicEntity();
        picEntity.picUrl = "http://pic.sc.chinaz.com/Files/pic/pic9/201804/zzpic11491_s.jpg";
        picEntity.originalPicUrl = "http://pic.sc.chinaz.com/Files/pic/pic9/201804/zzpic11491_s.jpg";
        picEntity.isGif = 0;
        picEntity.id = "1";
        picEntity.keyGifPicUrl = "http://pic.sc.chinaz.com/Files/pic/pic9/201804/zzpic11491_s.jpg";
        PicEntity picEntity1 = new PicEntity();
        picEntity1.picUrl = "http://pic.sc.chinaz.com/Files/pic/pic9/201805/zzpic11633_s.jpg";
        picEntity1.originalPicUrl = "http://pic.sc.chinaz.com/Files/pic/pic9/201805/zzpic11633_s.jpg";
        picEntity1.isGif = 0;
        picEntity1.id = "1";
        picEntity1.keyGifPicUrl = "http://pic.sc.chinaz.com/Files/pic/pic9/201805/zzpic11633_s.jpg";
        picEntities.add(picEntity);
        picEntities.add(picEntity1);
        ARouter.getInstance().
                build("/zhishu/GestureImageActivity").
                withInt(UrlName.POSITION,0).
                withInt(UrlName.pageFrom,0).
                withParcelableArrayList(UrlName.ENTITY_DATA,picEntities).
                navigation();
        return msg + "［syn call］";

    }

    @JavascriptInterface
    public void testAsyn(Object msg, CompletionHandler<String> handler) {

        // handler.complete(msg+" [ asyn call]");
    }

    @JavascriptInterface
    public String testNoArgSyn(Object arg) throws JSONException {
        return "testNoArgSyn called [ syn call]";
    }

    @JavascriptInterface
    public void testNoArgAsyn(Object arg, CompletionHandler<String> handler) {
        handler.complete("testNoArgAsyn   called [ asyn call]");
    }


    //@JavascriptInterface
    //without @JavascriptInterface annotation can't be called
    public String testNever(Object arg) throws JSONException {
        JSONObject jsonObject = (JSONObject) arg;
        return jsonObject.getString("msg") + "[ never call]";
    }

    @JavascriptInterface
    public void callProgress(Object args, final CompletionHandler<Integer> handler) {

        new CountDownTimer(11000, 1000) {
            int i = 10;

            @Override
            public void onTick(long millisUntilFinished) {
                //setProgressData can be called many times util complete be called.
                handler.setProgressData((i--));

            }

            @Override
            public void onFinish() {
                //complete the js invocation with data; handler will be invalid when complete is called
                handler.complete(0);

            }
        }.start();
    }

}
